<?php
namespace app\common\entity;


use think\Model;

class ShopUserAddress extends Model
{
    /**
     * @var string 对应的数据表名
     */
    protected $table = 'shop_user_address';

    protected $createTime = 'create_time';

    protected $autoWriteTimestamp = false;

    public function add($entity,$uid,$data)
    {
        $status = $data['status'] ? $data['status'] : 0;
        if ($status == 1) {

            $entity->where('uid', $uid)->setField('status', 0);
        }
        $isHas = $entity->where('uid', $uid)->find();
        if (!$isHas) {
            $status = 1;
        }

        $entity->uid = $uid;
        $entity->real_name = $data['real_name'];
        $entity->address = $data['address'];
        $entity->mobile = $data['mobile'];
        $entity->city_address = $data['city_address'];
        $entity->create_time = time();
        $entity->status = $status;
        return $entity->save();

    }

    public function upd($entity, $uid, $data)
    {
        $status = $data['status'] ? $data['status'] : 0;
        if ($status == 1) {

            $entity->where('uid', $uid)->setField('status', 0);
        }
        $info = $entity->where('id', $data['id'])->find();
        if ($info['uid'] != $uid) {
            return false;
        }
        if(!$info){
            return false;
        }

        $info->real_name = $data['real_name'];
        $info->address = $data['address'];
        $info->mobile = $data['mobile'];
        $info->city_address = $data['city_address'];
        $info->status = $status;
        $info->update_time = time();

        return $info->save();

    }

}